using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmSubjectDetail : Form
    {
        Business.PersonService personService = new Business.PersonService();
        Business.SubjectDetailService subjectDetailService = new Business.SubjectDetailService();
        Business.SubjectService subjectService = new Business.SubjectService();
        Business.ClassService classService = new Business.ClassService();
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.Person> personList = new List<Entities.Person>();
        List<Entities.Subject> subjectList = new List<Entities.Subject>();
        List<Entities.SubjectDetail> subjectDetailList = new List<Entities.SubjectDetail>();
        Entities.SubjectDetail subjectDetail = null;
        int iIndex = 0;

        private void InitializeClass()
        {
            classList = classService.GetAllClasses();
            foreach (Entities.Class _class in classList)
            {
                cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
            }
            cmbClass.SelectedIndex = 0;
        }

        private void InitializeSubject()
        {
            cmbSubject.Items.Clear();
            subjectList = subjectService.GetSubjectByClassID(cmbClass.Text.Substring(0, 6));
            foreach (Entities.Subject subject in subjectList)
            {
                cmbSubject.Items.Add(subject.SubjectID + "--" + subject.SubjectName);
            }
        }

        private void InitializeTrainer()
        {
            cmbTrainer.Items.Clear();
            personList = personService.GetPersonWithRoleID("R13");
            foreach (Entities.Person person in personList)
            {
                cmbTrainer.Items.Add(person.PersonID + "--" + person.PersonName);
            }
        }

        private void InitializeSubjectDetail()
        {
            if (dgvSubjectDetail.Rows.Count > 1)
            {
                int iCount = dgvSubjectDetail.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvSubjectDetail.Rows.RemoveAt(0);
                }
            }
            subjectDetailList = subjectDetailService.GetSubjectDetailByClassID(cmbClass.Text.Substring(0, 6));
            for (int i = 0; i < subjectDetailList.Count; i++)
            {
                string[] temp = { subjectDetailList[i].SubjectID, subjectDetailList[i].StaffID, subjectDetailList[i].ClassID };
                dgvSubjectDetail.Rows.Add(temp);
            }
            dgvSubjectDetail[0, iIndex].Selected = true;
        }

        private void FindSubjectDetail(string sSubjectID, string sClassID)
        {
            subjectDetail = new Entities.SubjectDetail();
            subjectDetail = subjectDetailService.GetSubjectDetailByClassIDAndSubjectID(sSubjectID, sClassID);
            cmbSubject.Text = subjectDetail.SubjectID;
            cmbTrainer.Text = subjectDetail.StaffID;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (cmbSubject.Text == string.Empty)
                sInformation += "SubjectID is empty" + "\n";
            if (cmbTrainer.Text == string.Empty)
                sInformation += "TrainerID is empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void InsertSubjectDetail()
        {
            if (Test())
            {
                Entities.SubjectDetail subjectDetail = new Entities.SubjectDetail();
                subjectDetail.ClassID = cmbClass.Text.Substring(0, 6);
                subjectDetail.SubjectID = (cmbSubject.Text.Length > 5 ? cmbSubject.Text.Substring(0, 5) : cmbSubject.Text);
                subjectDetail.StaffID = (cmbTrainer.Text.Length > 9 ? cmbTrainer.Text.Substring(0, 9) : cmbTrainer.Text);
                try
                {
                    subjectDetailService.InsertSubjectDetail(subjectDetail);
                    iIndex = dgvSubjectDetail.Rows.Count - 1;
                    InitializeSubjectDetail();
                }
                catch
                {
                    MessageBox.Show("This data have existed in database");
                }
            }
        }

        private void UpdateSubjectDetail()
        {
            if (Test())
            {
                subjectDetail.ClassID = cmbClass.Text.Substring(0, 6);
                subjectDetail.SubjectID = (cmbSubject.Text.Length > 5 ? cmbSubject.Text.Substring(0, 5) : cmbSubject.Text);
                subjectDetail.StaffID = (cmbTrainer.Text.Length > 9 ? cmbTrainer.Text.Substring(0, 9) : cmbTrainer.Text);
                try
                {
                    subjectDetailService.UpdateSubjectDetail(subjectDetail);
                    InitializeSubjectDetail();
                }
                catch
                {
                    MessageBox.Show("Update unsuccessfully");
                }
            }
        }

        private void DeleteSubjectDetail()
        {
            subjectDetailService.DeleteSubjectDetail(subjectDetail);
            if (iIndex > 0) iIndex--;
            else iIndex = 0;
            InitializeSubjectDetail();
            FindSubjectDetail(dgvSubjectDetail.Rows[iIndex].Cells[0].Value.ToString(), dgvSubjectDetail.Rows[iIndex].Cells[2].Value.ToString());
        }

        public FrmSubjectDetail()
        {
            InitializeComponent();
        }

        private void FrmSubjectDetail_Load(object sender, EventArgs e)
        {
            InitializeClass();
        }

        private void dgvSubjectDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvSubjectDetail.CurrentRow.Index < dgvSubjectDetail.Rows.Count - 1)
            {
                iIndex = dgvSubjectDetail.CurrentRow.Index;
                string sSubjectID = dgvSubjectDetail.CurrentRow.Cells[0].Value.ToString();
                string sClassID = dgvSubjectDetail.CurrentRow.Cells[2].Value.ToString();
                string sStaffID = dgvSubjectDetail.CurrentRow.Cells[1].Value.ToString();
                if (rdoUpdate.Checked)
                {
                    FindSubjectDetail(sSubjectID, sClassID);
                }
            }
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            iIndex = 0;
            InitializeSubject();
            InitializeTrainer();
            InitializeSubjectDetail();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (subjectDetail != null)
                UpdateSubjectDetail();
            else
                MessageBox.Show("You should choose the subject which you want to change!");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (subjectDetail != null)
            {
                if (MessageBox.Show("Do you really want to delete this file?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    DeleteSubjectDetail();
                }
            }
            else
                MessageBox.Show("You should choose the subjectDetail which you want to delete!");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertSubjectDetail();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdoInsert_CheckedChanged(object sender, EventArgs e)
        {
            cmbSubject.Enabled = true;
            cmbSubject.Text = string.Empty;
            cmbTrainer.Text = string.Empty;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void rdoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            cmbSubject.Enabled = false;
            cmbSubject.Text = string.Empty;
            cmbTrainer.Text = string.Empty;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            if(dgvSubjectDetail.Rows.Count > 1) FindSubjectDetail(dgvSubjectDetail.Rows[iIndex].Cells[0].Value.ToString(), dgvSubjectDetail.Rows[iIndex].Cells[2].Value.ToString());
        }
    }
}